package icu.zlz.emqx.service.impl;

import java.util.List;
import java.util.Map;

import icu.zlz.common.core.exception.ServiceException;
import icu.zlz.common.core.utils.AESUtil;
import icu.zlz.common.core.utils.DateUtils;
import icu.zlz.common.core.utils.RsaUtils;
import icu.zlz.common.core.utils.StringUtils;
import icu.zlz.common.core.utils.uuid.IdUtils;
import icu.zlz.common.core.web.domain.AjaxResult;
import icu.zlz.common.security.utils.SecurityUtils;
import icu.zlz.emqx.domain.IotGroup;
import icu.zlz.emqx.mapper.IotGroupMapper;
import icu.zlz.emqx.service.IIotGroupService;
import icu.zlz.emqx.utils.MqttTopicValidator;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import icu.zlz.emqx.mapper.IotTopicMapper;
import icu.zlz.emqx.domain.IotTopic;
import icu.zlz.emqx.service.IIotTopicService;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * 主题Service业务层处理
 *
 * @author zangs
 * @since 2024-12-31
 */
@Service
public class IotTopicServiceImpl implements IIotTopicService {
    @Resource
    private IotTopicMapper iotTopicMapper;
    @Resource
    private IotGroupMapper iotGroupMapper;
    @Resource
    private MqttAuthService mqttAuthService;

    @Resource
    private IIotGroupService iotGroupService;

    /**
     * 查询主题
     *
     * @param topicId 主题主键
     * @return 主题
     */
    @Override
    public IotTopic selectIotTopicByTopicId(Long topicId) {

        return iotTopicMapper.selectIotTopicByTopicId(topicId);
    }

    /**
     * 查询主题列表
     *
     * @param iotTopic 主题
     * @return 主题
     */
    @Override
    public List<IotTopic> selectIotTopicList(IotTopic iotTopic) {
        if (!SecurityUtils.isAdmin(SecurityUtils.getUserId())) {
            iotTopic.setUserId(SecurityUtils.getUserId());
        }
        return iotTopicMapper.selectIotTopicList(iotTopic);
    }

    /**
     * 新增主题
     *
     * @param iotTopic 主题
     * @return 结果
     */
    @Override
    public int insertIotTopic(IotTopic iotTopic) {
        iotGroupService.getIotGroupByGroupId(iotTopic.getGroupId());
        //校验主题名是否合法
        MqttTopicValidator.isValidMqttTopic(iotTopic.getTopicName());
        iotTopic.setCreateBy(SecurityUtils.getUsername());
        iotTopic.setCreateTime(DateUtils.getNowDate());
        iotTopic.setUid(IdUtils.fastSimpleUUID());
        return iotTopicMapper.insertIotTopic(iotTopic);
    }


    /**
     * 修改主题
     *
     * @param iotTopic 主题
     * @return 结果
     */
    @Transactional
    @Override
    public int updateIotTopic(IotTopic iotTopic) {
        if (ObjectUtils.isEmpty(iotTopic.getGroupId())) {
            throw new ServiceException("暂时不可修改所属群组");
        }
        MqttTopicValidator.isValidMqttTopic(iotTopic.getTopicName());

        IotGroup iotGroupByGroupId = iotGroupService.getIotGroupByGroupId(iotTopic.getGroupId());
        IotTopic old = iotTopicMapper.selectIotTopicByTopicId(iotTopic.getTopicId());
        String topicName = iotTopic.getTopicName();
        iotTopic.setUpdateBy(SecurityUtils.getUsername());
        iotTopic.setUpdateTime(DateUtils.getNowDate());
        iotTopic.setUid(null);

        int i = iotTopicMapper.updateIotTopic(iotTopic);

        if (StringUtils.hasText(topicName) && !topicName.equals(old.getTopicName())) {

            AjaxResult ajaxResult = mqttAuthService.editAclTopicByUid(old.getUid(), iotGroupByGroupId.getGroupPrefix() + "/" + topicName);
            if (!ajaxResult.isSuccess()) {
                throw new ServiceException("修改主题失败:" + ajaxResult.get(AjaxResult.MSG_TAG));

            }

        }
        return i;
    }

    /**
     * 批量删除主题
     *
     * @param topicIds 需要删除的主题主键
     * @return 结果
     */
    @Override
    public int deleteIotTopicByTopicIds(Long[] topicIds) {
        return iotTopicMapper.deleteIotTopicByTopicIds(topicIds);
    }

    /**
     * 删除主题信息
     *
     * @param topicId 主题主键
     * @return 结果
     */
    @Override
    public int deleteIotTopicByTopicId(Long topicId) {
        return iotTopicMapper.deleteIotTopicByTopicId(topicId);
    }


    @Override
    public List<Map<String, Object>> selectIotGroupList(String name) {
        if (SecurityUtils.isAdmin(SecurityUtils.getUserId())) {
            iotGroupMapper.selectIotGroupMapList(name, null);
        }
        return iotGroupMapper.selectIotGroupMapList(name, SecurityUtils.getUserId());
    }
}
